/*
 * @Descripttion: 
 * @version: 
 * @Author: zhaoxiaodong
 * @Date: 2020-09-01 10:08:02
 * @LastEditors  : 陈韵同
 * @LastEditTime : 2020-12-01 16:58:04
 */
const userKey = 'dcc_user'
const authKey = process.env.VUE_APP_authorization;
function getAuth() {
    const reg = new RegExp(`(?:(?:^|.*;\\s*)${authKey}\\s*\\=\\s*([^;]*).*$)|^.*$`);
    const val = document.cookie.replace(reg, "$1");
    return val
}
function getUserInfo() {
    const auth = getAuth()
    const localAuth = localStorage.getItem(authKey)
    if (auth && localAuth && auth === localAuth) {
        try {
            return JSON.parse(localStorage.getItem(userKey))
        } catch (error) {
            return {}
        }
    }
    return {}
}
export default {
    namespaced: true,
    state: {
        userInfo: getUserInfo(),
    },
    mutations: {
        SET_USERINFO(state, param) {
            localStorage.removeItem(userKey);
            localStorage.removeItem(authKey);
            localStorage.setItem(userKey, JSON.stringify(param));
            // 用一个随机数记录是否更新
            localStorage.setItem('userInfoNum', Math.random());
            localStorage.setItem(authKey, getAuth());
            state.userInfo = Object.assign({}, param);
        },
    },
    actions: {
        // 设置用户消息
        setUserInfo({ commit }, data) {
            commit("SET_USERINFO", data);
        },
    },
};
